<?php  if ( ! defined('APPPATH')) exit('No direct script access allowed');

// load database config
load_config('db');

class DB {

    private $host;
    private $name;
    private $user;
    private $pass;
    private $table;

    protected $sql;

    public $mysqli;
	protected $query_result;

    public function __construct() {
        $this->host = DB_HOST;
        $this->name = DB_NAME;
        $this->user = DB_USER;
        $this->pass = DB_PASS;
        $this->table = 'draw_users';
        $this->_connect();
    }

    protected function _connect() {
        $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name);
        if (mysqli_connect_error()) {
            die('Mysqli connect failed: '.mysqli_connect_error());
        }
        $this->mysqli->query("SET NAMES 'utf8'");
    }

	public function query($sql) {
		$this->query_result = $this->mysqli->query($sql);
		if (!$this->query_result) {
			return $this->query_result;
		}
		return $this;
	}

	public function result() {
		$rows = array();
        while($row = $this->query_result->fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
	}

    public function row() {
        return $this->query_result->fetch_assoc();
    }

    public function escape($str) {
        if (get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

    public function __destruct() {
        $this->mysqli->close();
    }
}